home *** CD-ROM | disk | FTP | other *** search
- Path: news.tamu.edu!tpradeep
- From: tpradeep@cs.tamu.edu (Pradeep K Tapadiya)
- Newsgroups: comp.lang.c++
- Subject: Exceptions and operator new. Help
- Date: 15 Feb 1996 01:09:30 GMT
- Organization: Texas A&M Computer Science Department, College Station, TX
- Message-ID: <4fu14a$2vg@news.tamu.edu>
- NNTP-Posting-Host: ftcl06.cs.tamu.edu
-
- According to the C++ specs, a new will throw a xalloc exception
- if memory allocation fails. The return value from new need not be
- NULL. In addition, the constructor can throw a different exception
- as defined by you, the programmer. This means, each time, I do a
- new, I have to do something like
-
- myObject* p = NULL;
-
- try {
- myObject* p = new myObject;
- }
- catch (xlloc e) {
- // do not delete p here since it was never allocated
- ...
- }
- catch (myException& e) {
- delete p;
- ...
-
-
- Moreover, if myObject's destructor has to do some
- memroy cleanup, for example,
-
- myObject::~myObject ()
- {
- delete [] m_pVarList;
- }
-
- then, the constructor will probably has to do something like
-
-
- myObject::myObject ()
- {
- try {
- m_pVarList = new char [10];
- }
- catch (xalloc e) {
- m_pVarList = NULL; // explicitly set it to NULL as destructor will
- // try to delete a bogus pointer
- throw xalloc ();
- }
- }
-
- Now, setnewHandler adds another variable to the model. As you cannot
- rely on what third party library is doing, anytime you do a new on
- your object, you will explictly have to do a setnewhandler to your handler.
-
- Though we have been programming in C++ for quite some time, this is
- the first time our company is venturing out into the world of
- exceptions. It now appears exceptions would make the development process
- more complicated.
-
- Am I missing something in the way I am using exceptions?
- Am I abusing exception handling mechanism?
- How are you handling exceptions with new opeartor?
- How are you integrating your code with third party library/dll?
-
- Thank you for enlightening me.
-
- Pradeep
-
-